package com.common.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.entity.Users;

public class LoginInterceptor implements HandlerInterceptor{
	/**
	 * 执行Handler完成之后执行。
	 */
	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}
	/**
	 * 进入Handler方法之后，返回ModelAndView之前执行
	 */
	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		// TODO Auto-generated method stub
		
	}
	
	/***
	 * 进入Handler方法之前执行。可以用于身份认证、身份授权
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object arg2) throws Exception {
		HttpSession session= request.getSession();
		Users user= (Users)session.getAttribute("User");
		Users Frontuser= (Users)session.getAttribute("FrontUser");
		System.out.println("拦截器 userSession:"+user);
		System.out.println("拦截器 Frontuser:"+Frontuser);
		String ss=request.getRequestURL().toString();
		String url= request.getContextPath();
		System.out.println("访问路径-------------------"+ss);
		if(ss.contains("front") || ss.contains("Front")){
			if(Frontuser==null){
				response.sendRedirect(url+"/frontlogin/register");
				return false;
			}else{
				return true;
			}
		}else{
			if(user==null){
				String path=request.getServletPath();
				if(path.contains("login")){
					return true;
				}
				response.sendRedirect(url+"/Showlogin");
				return false;
			}else{
				return true;
			}
		}
		
	}

}
